package com.suncnpap.reader.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.app.util.database.mybatis.Page;
import com.suncnpap.reader.service.ClassService;
import com.suncnpap.reader.mapper.ClassMapper;
import com.suncnpap.reader.model.Class;

/** 
 * 说明： 班级Service实现类
 * @version
 */
@Service("classService")
public class ClassServiceImpl implements ClassService{

	
	@Autowired
	ClassMapper classMapper;
	
	@Override
	public void add(Class obj) {
		// TODO Auto-generated method stub
		Integer maxSort = classMapper.findMaxSort(obj);
		maxSort =  maxSort==null?0:maxSort;
		obj.setIntSort(maxSort + 1);
		classMapper.add(obj);
	}

	@Override
	public void update(Class obj) {
		// TODO Auto-generated method stub
		if(obj.getIntOldSort() != null && obj.getIntOldSort().intValue() != 0 && obj.getIntOldSort().intValue() != obj.getIntSort()){
			classMapper.updateSortWhenUpdate(obj);
		}
		classMapper.edit(obj);
	}

	@Override
	public void deleteById(String strId) {
		// TODO Auto-generated method stub
		Class obj = classMapper.findById(strId);
		classMapper.deleteById(strId);
		classMapper.updateSortWhenDelete(obj);
		
	}
	@Override
	public void deleteMore(String[] strIds) {
		// TODO Auto-generated method stub
		for(int i=0;i<strIds.length;i++){
			Class obj = classMapper.findById(strIds[i]);
			classMapper.updateSortWhenDelete(obj);
		}
		classMapper.deleteMore(strIds);
	}

	@Override
	public Page list(Page page) {
		// TODO Auto-generated method stub
		List<Class> list = classMapper.datalistPage(page);
		page.setDataList(list);
		return page;
	}


	@Override
	public int findCount(Page page) {
		// TODO Auto-generated method stub
		return classMapper.findCount(page);
	}

	@Override
	public Class findById(String strId) {
		// TODO Auto-generated method stub
		return classMapper.findById(strId);
	}
	
	
	@Override
	public int findMaxSort(Class obj)  {
		Integer count=(Integer)classMapper.findMaxSort(obj);
		return count==null?0:count;
	}

}

